﻿#include"ObjectPool.h"
#include"ConcurentAlloc.h"
//void Alloc1()
//{
//	for (int i = 0; i < 5; i++)
//	{
//		void* ptr = ConcurrentAlloc(6);
//	}
//}
//void Alloc2()
//{
//	for (int i = 0; i < 5; i++)
//	{
//		void* ptr = ConcurrentAlloc(6);
//	}
//}
//void TLSTest()
//{
//	std::thread t1(Alloc1);
//	t1.join();
//	std::thread t2(Alloc2);
//	t2.join();
//}
void DeAlloc1()
{
	std::vector<void*> v;
	for (int i = 0; i < 7; i++)
	{
		void* p = ConcurrentAlloc(6);
		v.push_back(p);
	}
	for (auto e : v)
	{
		ConcurrentFree(e);
	}
}
void DeAlloc2()
{
	std::vector<void*> v;
	for (int i = 0; i < 7; i++)
	{
		void* p = ConcurrentAlloc(31);
		v.push_back(p);
	}
	for (auto e : v)
	{
		ConcurrentFree(e);
	}
}
void BigAlloc()
{
	void* p1 = ConcurrentAlloc(257 * 1024);
	ConcurrentFree(p1);

	void* p2 = ConcurrentAlloc(257 * 1024);
	ConcurrentFree(p2);
}
void BigAlloc1()
{
	std::vector<void*> v;
	for (int i = 0; i < 1000; i++)
	{
		void* p = ConcurrentAlloc(257*1024);
		v.push_back(p);
	}
	for (auto e : v)
	{
		ConcurrentFree(e);
	}
	//for (int i = 0;i < 3;i++)
	//{
	//	cout << "i ::" << i << endl;
	//	ConcurrentFree(v[i]);
	//}
}
//测试多线程环境下，central cache在一个哈希桶
//void MutilThreadTest1()
//{
//	std::thread t1(DeAlloc1);
//	std::thread t2(DeAlloc2);
//	t1.join();
//	t2.join();
//}
//测试多线程环境下，central cache在的不同哈希桶归还
void MutilThreadTest2()
{
	std::thread t1(DeAlloc1);
	std::thread t2(DeAlloc2);
	t1.join();
	t2.join();
}
void MutilThreadTest3()
{
	std::thread t1(DeAlloc1);
	t1.join();
}
void TestAlloc()
{
	
	ConcurrentAlloc(31);
	ConcurrentAlloc(9);
	ConcurrentAlloc(65);

}
void TestAllocPage()
{
	//
	for(int i = 0;i < 1024;i++)
		ConcurrentAlloc(6);
	ConcurrentAlloc(6);
	ConcurrentAlloc(6);

}
//void TestDealloc1()
//{
//	void* p1 = ConcurrentAlloc(6);
//	void* p2 = ConcurrentAlloc(6);
//	void* p3 = ConcurrentAlloc(6);
//	void* p4 = ConcurrentAlloc(6);
//	void* p5 = ConcurrentAlloc(6);
//
//	ConcurrentFree(p1, 6);
//	ConcurrentFree(p2, 6);
//	ConcurrentFree(p3, 6);
//	ConcurrentFree(p4, 6);
//	ConcurrentFree(p5, 6);
//}

void TestDealloc2()
{
	void* p1 = ConcurrentAlloc(6);
	void* p2 = ConcurrentAlloc(6);
	void* p3 = ConcurrentAlloc(6);
	void* p4 = ConcurrentAlloc(6);
	void* p5 = ConcurrentAlloc(6);
	void* p6 = ConcurrentAlloc(6);
	void* p7 = ConcurrentAlloc(6);


	ConcurrentFree(p1);
	ConcurrentFree(p2);
	ConcurrentFree(p3);
	ConcurrentFree(p4);
	ConcurrentFree(p5);
	ConcurrentFree(p6);
	ConcurrentFree(p7);
}
//int main()
//{
//	//测试定长内存池
//	//TestObjectPool();
//	//TLSTest();
//	//TestAllocPage();
//	//TestDealloc2();
//	//MutilThreadTest2();
//	BigAlloc1();
//	//TestAlloc();
//	return 0;
//}
